﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QBS.Tools
{
    public class JsonHelper
    {
        public int code { get { return _code; } set { _code = value; } }
        public string msg
        {
            get
            {
                if (String.IsNullOrEmpty(_msg))
                {
                    switch (code)
                    {
                        case 200: return "操作完成!";
                        case 403: return "权限不足!";
                        case 404: return "数据不存在或参数错误!";
                        case 500: return "操作异常!";
                        default: return "未知错误!";
                    }
                }
                return _msg;
            }
            set { _msg = value; }
        }

        public object data { get { return _data; } set { _data = value; } }
        public int total { get { return _total; } set { _total = value; } }
        public int page { get { return _page; } set { _page = value; } }
        public int records { get; set; }
        private int _code;
        private string _msg;
        private int _total;
        private int _page;
        private object _data;
        public void Set(int code, string msg = "", object data = null, int total = 0, int page = 0)
        {
            _code = code;
            if (!String.IsNullOrEmpty(msg))
                _msg = msg;
            if (data != null)
                _data = data;
            if (total != 0)
                _total = total;
            if (page != 0)
                _page = page;
        }
        public void Show(int code = 0, string msg = "")
        {
            if (!string.IsNullOrEmpty(msg))
                _msg = msg;
            if (code > 0)
                _code = code;
            HttpContext.Current.Response.Write(this.enJson());
            HttpContext.Current.Response.End();
        }
        public void Show(string token)
        {
            string buf = this.enJson();
            buf = Crypt.Encrypt(buf, token);
            HttpContext.Current.Response.Write(buf);
            HttpContext.Current.Response.End();
        }
    }
}